﻿using System.Collections;
using UnityEngine;

namespace Scripts
{
    //朝着目标点移动
    public class PathMove : MonoBehaviour
    {
        public Transform
            path;   //路径物体,
        private Transform
         targetPoint;   //移动的目标点
        public float
            speed=5f;

        private void Start()
        {
            targetPoint = path.GetChild(0);
            transform.LookAt(targetPoint);
        }

        private void Update()
        {
            //朝着目标点移动
            transform.position = Vector3.MoveTowards(transform.position, targetPoint.position, speed*Time.deltaTime);
            //如果达到目标点,更换目标点为下一个点
            if (transform.position==targetPoint.position)
            {
             
                int indexCurrent = targetPoint.GetSiblingIndex();//获取当前目标点的索引
                if (indexCurrent==path.childCount-1)//如果当前目标点已经是最后一个,就关闭这个组件
                {
                    MonsterCreater.DestoryMonster(this.gameObject);
                    return;
                }
                targetPoint = path.GetChild(indexCurrent+1);
                transform.LookAt(targetPoint);//朝向目标点
            }
        }
    }
}